var util=require('../../utils/util.js');
var foodcart = require('../../utils/foodcart.js')
var app = getApp();
Page({
  data: {
    foodtype: 0,
    num: 0,
    buycar_num: 0,
    totalMoney: 0,
    block: false,
    price: 12.43,
    arr: [],
    food:{},
    imghost: app.globalData.imghost,
    evlist:[],
    evnum:0,
  },
  onLoad: function(options) {
    var that=this;
    var food = options.food;
    food=JSON.parse(food);
    if (food.foodEvaluate){
      food.evrate = ((food.foodEvaluate.good ? food.foodEvaluate.good : 0) / food.foodEvaluate.tnum).toFixed(2);
    }else{
      food.evrate = 1
    }
    
    this.setData({ food: food})
    var ret = foodcart.getFoodlist();
     
    this.setData({
      buycar_num: ret.buycart.buynum,
      totalMoney: ret.buycart.buyamount
    })
    this.getEvaluate(food.id);
  },
  getEvaluate(foodid){
    var that=this;
    var url = util.url.user_evaluate_list;
    url +="?foodid="+foodid+"&pageNo=0"; 
    util.fetchGet(
      url,
      function(ret){
        var evlist=ret.data.content;
        for (var i=0;i<evlist.length;i++){
          evlist[i].createTime = (util.getNextTime(0,evlist[i].createTime)).substring(0,10);
          if (evlist[i].foods.length>1){
            var foods = evlist[i].foods;
            var foodname="";
            for (var j=0;j<foods.length;j++){
              foodname += foods[j].name+"+";
            }
            foodname = foodname.substring(0, foodname.length-1);
            evlist[i].foodname = foodname;
          }
        }
        that.setData({
          evnum: ret.data.numberOfElements,
          evlist: evlist
        })
      }
    )
  },
  resetNum: function (e) {
    var type = e.currentTarget.dataset.id;
    this.setData({
      foodtype: type
    })
  },
  reduce: function() {
    var ret=foodcart.addFoodCart(this.data.food.id,-1);
    if (this.data.food.num - 1<0){
      return;
    }
    this.setData({
      buycar_num: ret.buycart.buynum,
      totalMoney: ret.buycart.buyamount,
      'food.num':this.data.food.num-1
    })
  }, 
  add: function () {
    var ret = foodcart.addFoodCart(this.data.food.id, 1);
    this.setData({
      buycar_num: ret.buycart.buynum,
      totalMoney: ret.buycart.buyamount,
      'food.num': this.data.food.num + 1
    })
  },
  close: function() {
    this.setData({
      block: false
    })
  },
  open: function () {
    this.setData({
      block: true
    })
  },
  submit: function() {
    var that = this;
    var i = this.data.foodtype;
    var id;  //哪种食物
    wx.request({
      url: '', 
      data: {
        type: that.data.arr[i].value,
        num: that.data.num
      },
      success: function (res) {
        that.setData({
          block: false
        })
        console.log(res.data)
      }
    })
  },
  buyNow: function() {
    wx.navigateTo({
      url: '../submitOrder/submitOrder'
    })
  },
  toAllEvaluate: function() {
    util.showinfo("暂时只显示最近10条评论");
    return ;
    wx.navigateTo({
      url: '../allEvaluate/allEvaluate',
    })
  },
  toSubmit: function() {
    wx.navigateTo({
      url: '../submitOrder/submitOrder',
    })
  }
})